package com.example.adminserivce.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.adminserivce.Mapper.ActivityMapper;
import com.example.adminserivce.Service.ActivityService;
import com.example.adminserivce.Service.ScheduleService;
import com.example.adminserivce.Service.UserService;
import com.example.common.constants.OtherConstants;
import com.example.common.domain.pojo.Activity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@Service
@Slf4j
public class ScheduleServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ScheduleService {
    // 基准日期：2025-05-20 为第一周起始日
    private static final LocalDate BASE_DATE = LocalDate.of(OtherConstants.ACTIVE_YEAR,
            OtherConstants.ACTIVE_MONTH, OtherConstants.ACTIVE_DAY);//年，月，日
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private UserService userService;


    // 在ScheduleService中添加每日数据清理任务
    @Scheduled(cron = "0 0 0 1 * ?") // 每月1日凌晨清理30天前的日数据
    public void cleanOldDailyData() {
        LocalDate cutoffDate = LocalDate.now().minusDays(OtherConstants.ACTIVE_EVERY_DAY);  //之前30天
        log.info("开始清理30天之前的用户活跃度的数据");
        LambdaQueryWrapper<Activity> query = new LambdaQueryWrapper<>();
        query.le(Activity::getDate, cutoffDate);
        int count = (int) this.count(query);
        log.info("即将删除 {} 条数据", count);

        boolean result = this.remove(query);
        if (result) {
            log.info("成功删除 {} 条数据", count);
        } else {
            log.warn("删除操作未执行或失败");
        }
    }

}
